Service Worker
Owner: Nuwan Danushka
Introduction
The main app can register a service worker and be installed as a progressive web app (PWA). The framework provides the pieces around the service worker, but not the worker itself:
sw_reg.jsat the framework root registers and unregisters the worker.index.phploads it when the file exists.XP.initializeServiceWorker(), called on every page load, registers the worker when it's enabled inxp-config.jsonand unregisters it when it isn't.index.phplinks a web app manifest, and the admin panel can set the manifest's name, colours and icons.
The admin panel has no service worker.
How to add a service worker
-
Write your service worker and save it as
sw.jsin the framework root, besideindex.php. Nosw.jsships with the framework. -
Open
xp-config.jsonin the framework root. It's a single JSON file. See Configuration Files. -
In its
service_workerblock, setenable_service_workerto the string"true":"service_worker": {"enable_service_worker": "true","path": "/sw.js","version": "1.0.0","debug": "true"} -
Reload the page. The browser console shows
[SW] Service Worker registered successfully.
The worker is registered with scope /, so it controls the whole site.
How to update or remove the service worker
- Update: change
sw.js. The browser checks the file on navigation and installs the new version when its content differs. Changingversioninxp-config.jsondoes nothing. To check for an update at once, callwindow.updateServiceWorker()from the console or your code. - Remove: set
enable_service_workerto"false". On the next page load, every service worker registered for the site is unregistered, not only yours.
How the app is installed as a PWA
The manifest
index.php links the manifest from the API:
<link rel="manifest" href="/api/system/manifest?uri=/current/route">
The frontend updates the link on every route change. The endpoint is public and picks the manifest like this:
- If the route belongs to an app that has
apps/<app>/manifest.json, it returns that file. A route belongs to an app when it matches apathpassed toRouter.addRoute()in the app'sroute.js. - Otherwise it returns the system manifest set on the admin panel's Settings > Mobile App Branding page.
- If
<db_access>is off, it returns a default manifest named "DoFramework".
Icons
On Settings > Mobile App Branding, upload a PNG app icon. The framework resizes it to ten sizes, from 36×36 to 512×512, saves them in assets/pwa_icons/ (for example assets/pwa_icons/icon-192x192.png) and adds them to the system manifest. The same page sets the name, short name, language, theme and background colours, display mode, orientation and description.
Styling the installed app
When the app runs as an installed PWA (display mode standalone, fullscreen or minimal-ui), the frontend adds the class dc-pwa-mode to body. Use it to adjust the layout:
body.dc-pwa-mode .my-header {
padding-top: env(safe-area-inset-top);
}
To check it in code, call XP.device_detect.isRunningAsPWA(). See Frontend Runtime (XP).